﻿using JXC.Application.Dtos;
using JXC.Application.Services;
using JXC.Core.Models;
using JXC.Core.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.EntityFrameworkCore;
namespace JXC.Application.Services.UserInfoService
{
    public class UserInfoService:IUserInfoService
    {
        private readonly IRepository<UserInfos, int> _repository;
        private readonly IRepository<UserRoleInfos, int> _repositoryUserRole;
        private readonly IRepository<RoleInfos, int> _repositoryRole;
        public UserInfoService(IRepository<UserInfos, int> repository,IRepository<UserRoleInfos,int> repositoryUserRole,IRepository<RoleInfos,int> repositoryRole)
        {

            _repository = repository;
            _repositoryUserRole = repositoryUserRole;
            _repositoryRole = repositoryRole;
        }
        
        public async Task<PagedResultDto<UserInfos>> GetPagedResult(UserInfoDto input)
        {
            var query = _repository.GetAll();
            if (!string.IsNullOrEmpty(input.UserName))
            {
                query = query.Where(s => s.UserName.Contains(input.UserName));
            }
            if (!string.IsNullOrEmpty(input.UserState.ToString()))
            {
                query = query.Where(s => s.UserState.ToString().Equals(input.UserState));
            }

            var count = query.Count();
            //query = query.OrderBy(s=>input.Sorting).Skip((input.Page - 1) * input.Limit).Take(input.Limit);
            var stus=await query.AsNoTracking().ToListAsync();
            var dtos = new PagedResultDto<UserInfos>
            {
                TotalCount = count,
                Page = input.Page,
                Limit = input.Limit,
                Data = stus,
                FilterText = input.FilterText,
                Sorting = input.Sorting
            };
            return dtos;
        }
        //public object GetRoleList(Guid id)
        //{
        //    var role = _repositoryRole.GetAll();
        //    return role;
        //}
        /// <summary>
        /// 根据用户id在中间表获取拥有额角色id
        /// </summary>
        /// <param name="input"></param>
        /// <param name="count"></param>
        /// <returns></returns>

        public object GetUserTree(UserInfoDto input)
        {
            //通过用户id在中间表获取拥有额角色id
            var Infos = _repositoryUserRole.GetAllList(p => p.UserInfosId == input.id);            
            //通过中间表的角色id 在角色表查询该角色
            //var role = _repositoryRole.GetAll();
           // var user=_repository.GetAll().Include(u=>u.Id).Where(u=>u.)
            //list集合
            List<Guid> RoleIdList = new List<Guid>();
            foreach (var item in Infos)
            {
                RoleIdList.Add(item.RoleInfosId);
            }
            //count = RoleIdList.Count();
           
            return RoleIdList;
            //var query = _repository.GetAll();

        }
    }
}
